<?php


namespace App\Http\Controllers\Admin;


use App\Models\UserStatus;

class StatusController extends Controller
{
    /*
     * @api get /study-api/a/users/{userId}/status 用户学习状态
     * @version 0.0.1
     * @group admin
     *
     * @param path int userId 用户ID
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{}
     * }
     */
    public function index($userId)
    {
        /** @var UserStatus $statusObj */
        $statusObj = UserStatus::where('user_id', $userId)->first();
        if ($statusObj) {
            $status = [
                'testLevel' => $statusObj->getTestLevel(),
                'currentLevel' => $statusObj->getCurrentLevel(),
                'wordCount' => $statusObj->getWordCount()
            ];
        } else {
            $status = [
                'testLevel' => 0,
                'currentLevel' => 0,
                'wordCount' => 0
            ];
        }
        return $this->sendSuccessJson($status);
    }

    /*
     * @api post /study-api/a/users/{userId}/level 更新用户级别
     * @version 0.0.1
     * @group admin
     *
     * @param path int userId 用户ID
     * @param body int level 级别，1-3
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{}
     * }
     */
    public function updateLevel($userId)
    {
        $this->validate($this->request, [
            'level' => 'required|integer'
        ]);
        $level = $this->request->input('level');
        /** @var UserStatus $statusObj */
        $statusObj = UserStatus::where('user_id', $userId)->first();
        $statusObj->setCurrentLevel($level);
        $statusObj->save();
        return $this->sendSuccessJson();
    }
}